package com.dlwx.firefighting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.adapter.AddressAdapter;
import com.dlwx.firefighting.adapter.AreaAdapter;
import com.dlwx.firefighting.adapter.CityAdapter;
import com.dlwx.firefighting.bean.ProvinceBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddressSelectActivity extends BaseActivity {

    @BindView(R.id.province_recycler)
    LRecyclerView provinceRecycler;
    @BindView(R.id.city_recycler)
    LRecyclerView cityRecycler;
    @BindView(R.id.district_recycler)
    LRecyclerView districtRecycler;
    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    private AreaAdapter areaAdapter;
    private CityAdapter cityAdapter;
    private AddressAdapter provinceAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    List<ProvinceBean> datas;
    List<ProvinceBean.CityArrayBean> cityDatas;
    List<ProvinceBean.CityArrayBean.AreaArrayBean> areaDatas;
    private LRecyclerViewAdapter areaRecyclerViewAdapter;
    private LRecyclerViewAdapter cityRecyclerViewAdapter;
    private ProvinceBean provinceBean;
    private ProvinceBean.CityArrayBean cityArrayBean;
    private ProvinceBean.CityArrayBean.AreaArrayBean areaArrayBean;
    private Handler mHandler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String res= (String) msg.obj;
            List<ProvinceBean> beans=new Gson().fromJson(res,new TypeToken<ProvinceBean>(){}.getType());
            setDatas(beans);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_select);
        ButterKnife.bind(this);
        initView();
        String cityStr=getString(R.string.citylist);
        Log.d("CCC",cityStr);
//        readRaw();
        /*List<ProvinceBean> beans=new Gson().fromJson(cityStr,new TypeToken<ProvinceBean>(){}.getType());
        setDatas(beans);*/
        getDatas();
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("选取地址");
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.divider_height_6)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.app_bg_color)
                .build();
//
        provinceAdapter = new AddressAdapter(this);
        provinceAdapter.setDataList(new ArrayList<ProvinceBean>());
        provinceRecycler.setLayoutManager(new LinearLayoutManager(this));
        lRecyclerViewAdapter = new LRecyclerViewAdapter(provinceAdapter);
        provinceRecycler.setAdapter(lRecyclerViewAdapter);
        provinceRecycler.addItemDecoration(divider);
        provinceRecycler.setLoadMoreEnabled(false);
        provinceRecycler.setPullRefreshEnabled(false);
        provinceRecycler.refresh();
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                cityAdapter.clear();
                cityRecyclerViewAdapter.notifyDataSetChanged();
                areaAdapter.clear();
                areaRecyclerViewAdapter.notifyDataSetChanged();
                provinceBean=datas.get(position);
                cityDatas = provinceBean.getCityArray();
                cityAdapter.addAll(cityDatas);
                cityRecycler.refresh();
            }
        });
//
        cityAdapter = new CityAdapter(this);
        cityAdapter.setDataList(new ArrayList<ProvinceBean.CityArrayBean>());
        cityRecycler.setLayoutManager(new LinearLayoutManager(this));
        cityRecyclerViewAdapter = new LRecyclerViewAdapter(cityAdapter);
        cityRecycler.setAdapter(cityRecyclerViewAdapter);
        cityRecycler.addItemDecoration(divider);
        cityRecycler.setLoadMoreEnabled(false);
        cityRecycler.setPullRefreshEnabled(false);
        cityRecycler.refresh();
        cityRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                areaAdapter.clear();
                areaRecyclerViewAdapter.notifyDataSetChanged();
                cityArrayBean=cityAdapter.getDataList().get(position);
                areaDatas = cityArrayBean.getAreaArray();
                areaAdapter.addAll(areaDatas);
                districtRecycler.refresh();
            }
        });
//
        areaAdapter = new AreaAdapter(this);
        areaAdapter.setDataList(new ArrayList<ProvinceBean.CityArrayBean.AreaArrayBean>());
        districtRecycler.setLayoutManager(new LinearLayoutManager(this));
        areaRecyclerViewAdapter = new LRecyclerViewAdapter(areaAdapter);
        districtRecycler.setAdapter(areaRecyclerViewAdapter);
        districtRecycler.addItemDecoration(divider);
        districtRecycler.setLoadMoreEnabled(false);
        districtRecycler.setPullRefreshEnabled(false);
        districtRecycler.refresh();
        areaRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                areaArrayBean=areaAdapter.getDataList().get(position);
                Intent i=new Intent();
                i.putExtra("province",provinceBean);
                i.putExtra("city",cityArrayBean);
                i.putExtra("area",areaArrayBean);
                AddressSelectActivity.this.setResult(1,i);
                AddressSelectActivity.this.finish();

            }
        });
    }

    private void getDatas() {
        DialogUtils.showProgressDlg(this,"");
        OkGo.<String>post(Constant.CITY_LIST)
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
//                        DealResult dealResult = new DealResult();
//                        List<ProvinceBean> beans = dealResult.deal(AddressSelectActivity.this, response, new TypeToken<BaseBean<ProvinceBean>>() {
//                        }.getType());
//                        setDatas(beans);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(AddressSelectActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void setDatas(List<ProvinceBean> beans) {
        datas = beans;
        provinceAdapter.addAll(beans);
        provinceRecycler.refresh();

    }

    public void readRaw(){
        try {
            //获取文件中的内容
            InputStream inputStream=getResources().openRawResource(R.raw.citylist);
            //将文件中的字节转换为字符
            InputStreamReader isReader=new InputStreamReader(inputStream,"UTF-8");
            //使用bufferReader去读取字符
            BufferedReader reader=new BufferedReader(isReader);
            String out="";
            try {
                while((out=reader.readLine())!=null){
                    Log.d("从raw文件夹中读取到的数据:",out);
                   Message msg=mHandler.obtainMessage();
                    msg.obj=out;
                    mHandler.sendMessage(msg);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }


    @OnClick({R.id.title_left_one_btn, R.id.tv_middle_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.setResult(0,null);
                this.finish();
                break;
            case R.id.tv_middle_title:
                break;
        }
    }
}
